package com.sxt.practiceapplication.pager.indextabdetialpager;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.activities.course.CourseActivity;
import com.sxt.practiceapplication.base.BasePager;
import com.sxt.practiceapplication.common.Urls;
import com.sxt.practiceapplication.databinding.IndexTabDetailPagerLayoutBinding;
import com.sxt.practiceapplication.entity.course.CourseItem;
import com.sxt.practiceapplication.entity.course.CourseType;
import com.sxt.practiceapplication.entity.course.data.CourseListData;
import com.sxt.practiceapplication.utils.GsonBinder;
import com.sxt.practiceapplication.utils.LoginUser;
import com.sxt.practiceapplication.utils.okhttp.DefaultRealCallback;
import com.sxt.practiceapplication.utils.okhttp.OkHttpUtils;
import com.sxt.practiceapplication.utils.okhttp.url.Param;
import com.sxt.practiceapplication.utils.okhttp.url.URL;
import com.sxt.practiceapplication.utils.okhttp.WrapResponse;
import com.sxt.practiceapplication.view.RefreshListView;

import org.xutils.x;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;


/**
 *
 */
public class IndexTabDetailPager extends BasePager {

    public CourseType courseType;

    private RefreshListView refreshListView;

    private List<CourseItem> courseItems;

    public IndexTabDetailPager(Context context) {
        this(context, null);
    }

    public IndexTabDetailPager(Context context, CourseType courseType) {
        super(context);
        this.courseType = courseType;
    }

    protected View initView() {
//        itemLayoutBinding = ImageItemLayoutBinding.inflate(LayoutInflater.from(context));
        com.sxt.practiceapplication.databinding.IndexTabDetailPagerLayoutBinding indexTabDetailPagerLayoutBinding = IndexTabDetailPagerLayoutBinding.inflate(LayoutInflater.from(context));
        refreshListView = indexTabDetailPagerLayoutBinding.detailList;
        refreshListView.setOnRefreshListener(IndexTabDetailPager.this::reloadData);

        // 点击某个视频item时的回调
        refreshListView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(context, CourseActivity.class);
            intent.putExtra("course", courseItems.get(position - 1));
            if (LoginUser.checkAndToLogin(context, intent)) {
                context.startActivity(intent);
            }
        });

        this.content = indexTabDetailPagerLayoutBinding.getRoot();
        return indexTabDetailPagerLayoutBinding.getRoot();
    }

    public void doInitData() {
//        + "?type=" + courseType.getId() + "&ordering=-id"
        OkHttpUtils.getInstance().get(new URL(Urls.COURSE_URL,
                        new Param<>("type", courseType.getId()),
                        new Param<>("limit", 100),
                        new Param<>("ordering", "-id")),
                new DefaultRealCallback(this, context) {
                    @Override
                    public void onResponse(Call call, WrapResponse response) {

                        CourseListData courseListData = GsonBinder.toObj(response.getBody(), CourseListData.class);
                        courseItems = courseListData.getData().getResults();
                        refreshListView.setAdapter(new BaseAdapter() {
                            @Override
                            public int getCount() {
                                return courseItems.size();
                            }

                            @Override
                            public Object getItem(int position) {
                                return courseItems.get(position);
                            }

                            @Override
                            public long getItemId(int position) {
                                return courseItems.get(position).getId();
                            }

                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                ViewHolder viewHolder;
                                if (convertView == null) {
//                                convertView = itemLayoutBinding.getRoot();
                                    convertView = LayoutInflater.from(context).inflate(R.layout.image_item_layout, parent, false);
                                    viewHolder = new ViewHolder();
                                    viewHolder.courseCover = convertView.findViewById(R.id.item_image);
                                    viewHolder.courseTitle = convertView.findViewById(R.id.course_item_title);
                                    viewHolder.courseDes = convertView.findViewById(R.id.course_item_des);
                                    convertView.setTag(viewHolder);
                                } else {
                                    viewHolder = (ViewHolder) convertView.getTag();
                                }
                                CourseItem courseItem = courseItems.get(position);
                                Glide.with(context)
                                        .load(courseItem.getCourseCover())
                                        .error(R.drawable.default_cover)
                                        .into(viewHolder.courseCover);
                                viewHolder.courseTitle.setText(courseItem.getCourseTittle());
                                viewHolder.courseDes.setText(courseItem.getCourseDes());
                                return convertView;
                            }
                        });

                        //隐藏下拉刷新空间并更新时间
                        refreshListView.onRefreshFinish(true);
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        refreshListView.removeAllViewsInLayout();
                        //隐藏下拉刷新控件，不更新时间
                        refreshListView.onRefreshFinish(false);
                    }
                });
    }

    static final class ViewHolder {
        TextView courseTitle;
        TextView courseDes;
        ImageView courseCover;
    }

}
